import { defineStore } from 'pinia';
import { defaultRoute } from '@/util/menuConfig'

export const navTabStore = defineStore({
    id: 'navTab',
    state() {
        return {
            visitedRoutes: [],
            // 顶部右侧菜单-高亮当前路由
            activeRouterName: defaultRoute
        }
    },
    actions: {
      changeActiveRouterName(name) {
        this.activeRouterName = name;
        // localStorage.setItem('activeRouterName', name)
      },
      deleteActiveRouterName() {
        this.activeRouterName = defaultRoute;
        // localStorage.removeItem('activeRouterName')
      },
        // 添加路由
        addVisitedRoute(route) {
            let target = this.visitedRoutes.find((item) => item.path === route.path);
            if (target) {
                if (route.fullPath !== target.fullPath) Object.assign(target, route);
                return;
            }
            this.visitedRoutes.push(Object.assign({}, route));
        },
        // 删除单个路由
        delVisitedRoute(path) {
            this.visitedRoutes.forEach((item, index) => {
                if (item.path === path) this.visitedRoutes.splice(index, 1);
            });
        },
        // 删除其他路由
        delOthersVisitedRoute(path) {
            this.visitedRoutes = this.visitedRoutes.filter(
                (item) => item.path === path
            );
        },
        // 删除左侧路由
        delLeftVisitedRoute(path) {
            let index = this.visitedRoutes.length;
            this.visitedRoutes = this.visitedRoutes.filter((item) => {
                if (item.path === path) index = this.visitedRoutes.indexOf(item);
                return index <= this.visitedRoutes.indexOf(item);
            });
        },
        // 删除右侧路由
        delRightVisitedRoute(path) {
            let index = this.visitedRoutes.length;
            this.visitedRoutes = this.visitedRoutes.filter((item) => {
                if (item.path === path) index = this.visitedRoutes.indexOf(item);
                return index >= this.visitedRoutes.indexOf(item);
            });
        },
    },
    persist: {
        enabled: true
    }
})